package org.dromara.module.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.module.domain.CnCompany;
import org.dromara.module.domain.CnPartner;
import org.dromara.module.service.ICnCompanyService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 企业
 *
 * @author Lion Li
 * @date 2025-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/company")
public class CnCompanyController extends BaseController {

    private final ICnCompanyService cnCompanyService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;

    @Value("${ruoyi.profile}")
    private String uploadPath;

    /**
     * 查询企业列表
     */
    @SaCheckPermission("module:company:list")
    @GetMapping("/list")
    public TableDataInfo<CnCompany> list(CnCompany cnCompany, PageQuery pageQuery) {
        QueryWrapper<CnCompany> qw = new QueryWrapper<>();

        TableDataInfo<CnCompany> td = PageUtils.buildDataInfo(cnCompanyService.page(PageUtils.buildPage(),qw));
        List<CnCompany> list = td.getRows();
        for (CnCompany company : list){
            if(StrUtil.isNotEmpty(company.getLogo())){
                company.setLogoPath(imgUrl + company.getLogo());
            }
        }
        return td;
    }

    /**
     * 导出企业列表
     */
    @SaCheckPermission("module:company:export")
    @Log(title = "企业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CnCompany cnCompany, HttpServletResponse response) {
        List<CnCompany> list = cnCompanyService.list();
        ExcelUtil.exportExcel(list, "企业", CnCompany.class, response);
    }

    /**
     * 获取企业详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("module:company:query")
    @GetMapping("/{id}")
    public R<CnCompany> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        CnCompany company = cnCompanyService.getById(id);
        if(company != null){
            if(StrUtil.isNotEmpty(company.getLogo())){
                company.setLogoPath(imgUrl + company.getLogo());
            }
        }
        return R.ok(company);
    }

    /**
     * 新增企业
     */
    @SaCheckPermission("module:company:add")
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CnCompany cnCompany) {
        return toAjax(cnCompanyService.save(cnCompany));
    }

    /**
     * 修改企业
     */
    @SaCheckPermission("module:company:edit")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CnCompany cnCompany) {
        return toAjax(cnCompanyService.updateById(cnCompany));
    }

    /**
     * 删除企业
     *
     * @param ids 主键串
     */
    @SaCheckPermission("module:company:remove")
    @Log(title = "企业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(cnCompanyService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
